package com.egame.manage.biz.web.controller.gc;

import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.write.WritableWorkbook;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import cn.egame.common.model.PageData;
import cn.egame.common.servlet.WebUtils;
import cn.egame.common.util.Utils;
import cn.egame.ext.EnumTypeExt.GameDownRankType;
import cn.egame.interfaces.EnumType.GameStatus;
import cn.egame.interfaces.EnumType.GameType;
import cn.egame.interfaces.EnumType.NetworkType;
import cn.egame.interfaces.EnumType.PayType;
import cn.egame.interfaces.fl.FileUsedType;
import cn.egame.interfaces.gc.GameChargeInfo;
import cn.egame.interfaces.gc.GameInfo;
import cn.egame.interfaces.pu.AppParameter;

import com.egame.manage.biz.domain.AuditInfo;
import com.egame.manage.biz.domain.ImageInfo;
import com.egame.manage.biz.server.ManageClient;
import com.egame.manage.biz.server.gc.AuditServiceImpl;
import com.egame.manage.biz.server.gc.GameChargeServiceImpl;
import com.egame.manage.biz.server.gc.GameFileServiceImpl;
import com.egame.manage.biz.server.gc.GameServiceImpl;
import com.egame.manage.biz.server.gc.GameTagServiceImpl;
import com.egame.manage.biz.utils.ConstVar;
import com.egame.manage.biz.utils.ExcelUtils;
import com.egame.manage.biz.web.controller.BaseController;
import com.lenovo.manager.business.domain.UserBean;

@Controller
@RequestMapping("/caidan/game")
public class StoreGameController  extends BaseController{

	public StoreGameController() {
	}
	public StoreGameController(HttpServletRequest req, HttpServletResponse resp) {
		super(req, resp);
	}
	
	@RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView listGameInfo(HttpServletRequest req, HttpServletResponse resp) throws Exception{	
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int page = WebUtils.getInt(req,"page",0);
		int rowsOfPage = WebUtils.getInt(req,"rowsOfPage",15);				
		String shortName = WebUtils.getString(req, "shortName", "");
		String gameName = WebUtils.getString(req, "gameName", "");
		int gamestatus = WebUtils.getInt(req,"gamestatus",GameStatus.none.value());
		ModelAndView mv = new ModelAndView("/new/op/store/gameList");	
		
		GameInfo bean = new GameInfo();
		bean.setGameStatus(GameStatus.lookup(gamestatus));
		
		bean.setGameName(gameName);			
		PageData pd = GameServiceImpl.getInstance().pageGameList(appId, loginUserId,bean,shortName, page, rowsOfPage);
		mv.addObject("pd", pd);
		mv.addObject("pdUrl", "/caidan/game/list");		
		mv.addObject("status", gamestatus);
		mv.addObject("shortName", shortName);
		mv.addObject("gameName", gameName);
		
		return mv;
	}
	
	@RequestMapping(value = "/show", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView showGameInfo(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gid = WebUtils.getInt(req,"gid",0);		
		ModelAndView mv = new ModelAndView("/new/op/store/gameInfo");		
		GameInfo bean = GameServiceImpl.getInstance().getGameInfoById(appId, loginUserId, gid);
		mv.addObject("bean", bean);
		
		String[] filePath = null;
		if(!Utils.stringIsNullOrEmpty(bean.getStrategy())){
			filePath = GameServiceImpl.getInstance().getFilePath(appId, loginUserId, Utils.toLong(bean.getStrategy(), 0));
			if(filePath!=null){
				mv.addObject("stratery", filePath);
			}
		}
		if(!Utils.stringIsNullOrEmpty(bean.getGameDemoPath())){
			filePath = GameServiceImpl.getInstance().getFilePath(appId, loginUserId, Utils.toLong(bean.getGameDemoPath(), 0));
			if(filePath!=null){
				mv.addObject("demo", filePath);
			}
		}
		if(!Utils.stringIsNullOrEmpty(bean.getCopyrightPath())){
			filePath = GameServiceImpl.getInstance().getFilePath(appId, loginUserId, Utils.toLong(bean.getCopyrightPath(), 0));
			if(filePath!=null){
				mv.addObject("copyright", filePath);
			}
		}
		
		List<Long> imageIds = ManageClient.getInstance().listGameImageIdsByGId(appId, loginUserId, gid);
		List<ImageInfo> imageList = new ArrayList<ImageInfo>();		
		List<String> nameList = new ArrayList<String>();
		ImageInfo image = null;
		ImageInfo smallImage = null;
		for(Long id:imageIds){
			image = GameServiceImpl.getInstance().getImageInfoById(gid, loginUserId, id);
			if(image != null){
				imageList.add(image);
				nameList.add(image.getName());
			}			
			if(image != null && image.getFileUsedType() == FileUsedType.game_photo){
				smallImage = GameServiceImpl.getInstance().getSmallImageInfoById(gid, loginUserId, id);
				if(smallImage != null){
					imageList.add(smallImage);
					nameList.add(smallImage.getName());
				}
			}
			
		}	
		Collections.sort(imageList);
//		List<ImageInfo> resultList = new ArrayList<ImageInfo>();		
//		for(String name:nameList){
//			for(ImageInfo imageInfo:imageList){
//				if(name.equals(imageInfo.getName())){
//					resultList.add(imageInfo);
//				}
//			}
//		}
		mv.addObject("images", imageList);
		
		List<Integer> chargeIds = ManageClient.getInstance().listChargeIdsByGId(appId, loginUserId, gid);
		List<GameChargeInfo> charges = ManageClient.getInstance().listGameChargeInfoByChargeIds(appId, loginUserId, chargeIds);
		mv.addObject("charges", charges);
		
		List<AuditInfo> audits = AuditServiceImpl.getInstance().listAuditInfoByGid(gid);
		mv.addObject("audits", audits);
		
		return mv;
	}
	
	@RequestMapping(value = "/list/manage", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView listGameManage(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int page = WebUtils.getInt(req,"page",0);
		int rowsOfPage = WebUtils.getInt(req,"rowsOfPage",15);
		int gamestatus = WebUtils.getInt(req,"gamestatus",GameStatus.none.value());
		int networktype = WebUtils.getInt(req,"networktype",0);
		String shortName = WebUtils.getString(req, "shortName", "");
		String gameName = WebUtils.getString(req, "gameName", "");
		
		GameInfo bean = new GameInfo();
		bean.setGameStatus(GameStatus.lookup(gamestatus));
		bean.setNetworkType(NetworkType.lookup(networktype));
		bean.setGameName(gameName);			
		bean.setGameType(GameType.shop);
		ModelAndView mv = new ModelAndView("/new/op/store/gameManageList");
		mv.addObject("shortName", shortName);
		mv.addObject("gameName", gameName);
		mv.addObject("gamestatus", gamestatus);
		mv.addObject("netType", networktype);
		List<Integer> gids = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.APP_GAME_STATUS);
		List<AppParameter> statusList = ManageClient.getInstance().listAppParameterByIds(appId, loginUserId, gids);
		mv.addObject("statusList", statusList);		
		
		List<Integer> networktypeIds = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.APP_NETWORK);
		List<AppParameter> networktypeList = ManageClient.getInstance().listAppParameterByIds(appId, loginUserId, networktypeIds);
		mv.addObject("networktype", networktypeList);
		
		PageData pd = GameServiceImpl.getInstance().pageGameList(appId, loginUserId, bean, shortName, page, rowsOfPage);
		mv.addObject("pd", pd);
		mv.addObject("pdUrl", "/caidan/game/list/manage");
		
		Map<Integer, Integer> chargeMap = new HashMap<Integer, Integer>();
		if(pd != null && pd.getContent() != null){
			List<GameInfo> gameList = (List<GameInfo>)pd.getContent();
			int chargeCount = 0;
			for(GameInfo gameBean:gameList){
				chargeCount = ManageClient.getInstance().getChargeCount(rowsOfPage, gamestatus, gameBean.getGId());
				chargeMap.put(gameBean.getGId(), chargeCount);
			}
		}
		mv.addObject("chargeMap", chargeMap);
		mv.addObject("selectedStatus", gamestatus);
		mv.addObject("selectedNetwork", networktype);
		return mv;
	}
		
	@RequestMapping(value = "/label", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView toGameLabel(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gid = WebUtils.getInt(req,"gid",0);		
		ModelAndView mv = new ModelAndView("/new/op/store/gameLabel");
		
		String gname =  req.getParameter("gname")==null?"":req.getParameter("gname");		
		GameInfo gameInfo = new GameInfo();
		gameInfo.setGameName(gname);
		gameInfo.setGameStatus(GameStatus.business);
		List<GameInfo> list = GameServiceImpl.getInstance().listGameList(appId, loginUserId, gameInfo);
		mv.addObject("gameList", list);		
		
		GameInfo bean = GameServiceImpl.getInstance().getGameInfoById(appId, loginUserId, gid);		
		mv.addObject("bean", bean);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping(value = "/update/label", method = {RequestMethod.GET,RequestMethod.POST})
	public int updateGameLabel(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gid = WebUtils.getInt(req,"gid",0);		
		String gamelabel = req.getParameter("label")==null?"":req.getParameter("label");
		return GameServiceImpl.getInstance().updateGameLabel(appId, loginUserId, gid, gamelabel);		
	}
	
	@RequestMapping(value = "/update/status", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView updateGameStatus(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		int gid = WebUtils.getInt(req,"gid",0);	
		int gamestatus = WebUtils.getInt(req,"updatestatus",0);
		updateStatus(appId, loginUserId, gid,gamestatus);
		String url = WebUtils.getString(req, "url","/caidan/game/list/manage");		
		if("/caidan/game/list/manage".equals(url)){
			return listGameManage(req,resp);
		}else{
			return listGameInfo(req,resp);
		}
	}

	@RequestMapping(value = "/update/status/list", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView updateGameStatusList(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		String[] gids = req.getParameterValues("gid");		
		int updatestatus = WebUtils.getInt(req,"updatestatus",0);	
		String url = WebUtils.getString(req, "url","/caidan/game/list/manage");
		GameInfo game = null;
		if(gids != null && gids.length>0){
			for(String gid:gids){	
				game = GameServiceImpl.getInstance().getGameInfoById(appId, loginUserId, Integer.valueOf(gid));
				if(game != null && game.getGameStatus() == GameStatus.offLine && updatestatus == GameStatus.business.value()){
					updateStatus(appId, loginUserId, Integer.valueOf(gid), updatestatus);
				}else if(game != null && game.getGameStatus() == GameStatus.business && updatestatus == GameStatus.offLine.value()){
					updateStatus(appId, loginUserId, Integer.valueOf(gid), updatestatus);
				}
			}
		}			
		if("/caidan/game/list/manage".equals(url)){
			return listGameManage(req,resp);
		}else{
			return listGameInfo(req,resp);
		}
	}
	private void updateStatus(int appId, long loginUserId,int gid,int gamestatus)throws Exception{		
		GameInfo bean = GameServiceImpl.getInstance().getGameInfoById(appId, loginUserId, gid);		
		GameServiceImpl.getInstance().updateGameStatus(appId, loginUserId, gid, gamestatus);
		AuditServiceImpl.getInstance().setAuditLog(gid,gamestatus,loginUserId);
		if(gamestatus == GameStatus.business.value()){
			GameTagServiceImpl.getInstance().enableTagLinkByGid(gid);
			if(bean != null){				
				if(bean.getFirstOnlineTime()==0){
					GameServiceImpl.getInstance().updateGameFirstOnLineTime(gid, new Date().getTime());
				}else{
					GameServiceImpl.getInstance().updateGameLastOnLineTime(gid, new Date());
				}
			}			
		}else if(gamestatus == GameStatus.offLine.value()){
			GameServiceImpl.getInstance().updateGameLastOffLineTime(gid, new Date());	
			GameTagServiceImpl.getInstance().disableTagLinkByGid(gid);
		}else if(gamestatus == GameStatus.published.value()){
			if(bean != null){
				if(bean.getPublishTime() != null && bean.getPublishTime().getTime()==0){
					GameServiceImpl.getInstance().updateGamePublishTime(gid, new Date());
				}
			}		
		}
	}
	
	@RequestMapping(value = "/new/show", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView showNewGame(HttpServletRequest req, HttpServletResponse resp) throws Exception{	
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		ModelAndView mv = new ModelAndView("/new/op/store/newGame");		
		Map<String, Object> cpMap = GameServiceImpl.getInstance().listCP();
		mv.addObject("cpMap", cpMap);
		
		List<Integer> networktypeIds = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.APP_NETWORK);
		List<AppParameter> networktype = ManageClient.getInstance().listAppParameterByIds(appId, loginUserId, networktypeIds);
		mv.addObject("networktype", networktype);
		
		List<Integer> productIDs = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.APP_PRODUCT_TYPE);
		List<AppParameter> producttype = ManageClient.getInstance().listAppParameterByIds(appId, loginUserId, productIDs);
		mv.addObject("producttype", producttype);
		
		List<Integer> ostypeIds = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.APP_OS);
		List<AppParameter> ostype = ManageClient.getInstance().listAppParameterByIds(appId, loginUserId, ostypeIds);
		mv.addObject("ostype", ostype);
		
		List<Integer> paytypeIDs = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.APP_PAY_TYPE);
		List<AppParameter> paytype = ManageClient.getInstance().listAppParameterByIds(appId, loginUserId, paytypeIDs);
		mv.addObject("paytype", paytype);
		
		List<Integer> onlinepaytypeIDs = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.APP_ONLINE_PAY_TYPE);
		List<AppParameter> onlinepaytype = ManageClient.getInstance().listAppParameterByIds(appId, loginUserId, onlinepaytypeIDs);
		mv.addObject("onlinepaytype", onlinepaytype);
		
		List<Integer> feetypeIDs = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.APP_FEE_TYPE);
		List<AppParameter> feetype = ManageClient.getInstance().listAppParameterByIds(appId, loginUserId, feetypeIDs);
		mv.addObject("feetype", feetype);
		
		List<Integer> chargeTypeIDs = ManageClient.getInstance().listAppParameterIdsByParentId(appId, loginUserId, ConstVar.APP_CHARGE_TYPE);
		List<AppParameter> chargeType = ManageClient.getInstance().listAppParameterByIds(appId, loginUserId, chargeTypeIDs);
		mv.addObject("chargetype", chargeType);
		
		return mv;
	}
	
	@ResponseBody
	@RequestMapping(value = "/manufacturer/search", method = {RequestMethod.GET,RequestMethod.POST})
	public Map<String,Object> searchManufacturer(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		String shortName = WebUtils.getString(req, "popShortName","");
		Map<String,Object> searchManu = GameServiceImpl.getInstance().listCpIdAndName(shortName);
		return searchManu;
	}
	
	@RequestMapping(value = "/new", method = {RequestMethod.GET,RequestMethod.POST})
	public String setGameInfo(long efsIdjietu1,long efsIdjietu2,long efsIdjietu3,long efsIdjietu4,long efsIdpic1,GameInfo bean,int terId,HttpServletRequest req, HttpServletResponse resp) throws Exception{			
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		//保存游戏信息
		bean.setCPId(terId);			
		bean = saveGameInfo(efsIdjietu1,efsIdjietu2,efsIdjietu3,efsIdjietu4,efsIdpic1,appId, loginUserId,bean,req);
		
		//产品分类		
		int tagid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, bean.getProductCode());
		GameTagServiceImpl.getInstance().addTagLink(appId, loginUserId, bean.getGId(), tagid, 9999 ,-1);
		
		//排行榜
		int dayid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, GameDownRankType.day.value());
		GameTagServiceImpl.getInstance().addTagLink(appId, loginUserId, bean.getGId(), dayid, 0, 0);
		int weekid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, GameDownRankType.week.value());
		GameTagServiceImpl.getInstance().addTagLink(appId, loginUserId, bean.getGId(), weekid, 0, 0);
		int monthid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, GameDownRankType.month.value());
		GameTagServiceImpl.getInstance().addTagLink(appId, loginUserId, bean.getGId(), monthid, 0, 0);
		int totalid = ManageClient.getInstance().getGameTagIdByAppParameterId(appId, loginUserId, GameDownRankType.total.value());
		GameTagServiceImpl.getInstance().addTagLink(appId, loginUserId, bean.getGId(), totalid, 0, 0);
		
		//保存道具信息
		String charges = req.getParameter("propInfo");
		int price = 0;
		if(!Utils.stringIsNullOrEmpty(charges)){
			String[] chargeArray = charges.split("%");
			for(String charge:chargeArray){
				price = saveGameChargeInfo(charge,"/",loginUserId,bean.getGId());
			}
		}
		//下载计费更新价格
		if(bean.getPayCode() == PayType.downloadPay.value() 
				|| bean.getPayCode() == PayType.payPerDownload.value()
				|| price>0 ){
			GameServiceImpl.getInstance().updatePriceByGid(appId,loginUserId,bean.getGId(),price);
		}
		return "redirect:/caidan/game/list/manage.do";
	}
	
	private int saveGameChargeInfo(String chargeInfo,String split,long loginUserId,int gid)throws Exception{
		if(!Utils.stringIsNullOrEmpty(chargeInfo) || !Utils.stringIsNullOrEmpty(split)){
			String[] chargeArray = chargeInfo.split(split);
			GameChargeInfo charge = new GameChargeInfo();
			charge.setgId(gid);
			charge.setChargeName(chargeArray[0]);
			charge.setFunction(chargeArray[1]);
			charge.setTrigerCondition(chargeArray[2]);
			charge.setBugGuide(chargeArray[3]);
			charge.setPrice(Utils.toInt(chargeArray[4], 0));
			charge.setChargeType(Utils.toInt(chargeArray[5], 0));
			charge.setOperatorId(loginUserId);
			GameChargeServiceImpl.getInstance().setGameChargeInfo(charge);
			return charge.getPrice();
		}
		return 0;
	}
	
	public GameInfo saveGameInfo(long efsIdjietu1,long efsIdjietu2,long efsIdjietu3,long efsIdjietu4,long efsIdpic1,int appId,long loginUserId,GameInfo bean,HttpServletRequest req)throws Exception{
				
		bean.setGameType(GameType.shop);
		bean.setGameStatus(GameStatus.toAudit);
		bean.setGameCode(GameServiceImpl.getInstance().generateGameCode());	
		bean.setInsertTime(new Date());		
		bean.setGameStatus(GameStatus.business);
		ManageClient.getInstance().setGameInfo(appId, loginUserId, bean);
		int gameId = ManageClient.getInstance().getGameIdByGameCode(appId, loginUserId, bean.getGameCode());		
		bean.setGId(gameId);
		GameServiceImpl.getInstance().setGameImage(appId, loginUserId, efsIdjietu1, gameId);
		GameServiceImpl.getInstance().setGameImage(appId, loginUserId, efsIdjietu2, gameId);
		GameServiceImpl.getInstance().setGameImage(appId, loginUserId, efsIdjietu3, gameId);
		GameServiceImpl.getInstance().setGameImage(appId, loginUserId, efsIdjietu4, gameId);
		GameServiceImpl.getInstance().setGameImage(appId, loginUserId, efsIdpic1, gameId);
		
		return bean;
	}
	
	@RequestMapping(value = "/update/label/import", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView updateGameLabelImport(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		MultipartFile mulfile = ((MultipartHttpServletRequest)req).getFile("filePath");
		Map<Integer,String> map = new HashMap<Integer,String>();
		map = ExcelUtils.ImportExcel(mulfile.getInputStream());
		GameServiceImpl.getInstance().updateGameLabels(appId, loginUserId, map);
		return toGameLabel(req,resp);
	}
	
	@RequestMapping(value = "/update/label/export", method = {RequestMethod.GET,RequestMethod.POST})
	public ModelAndView GameLabelExport(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		ModelAndView mv = new ModelAndView();
		
		resp.reset();
		resp.setHeader("Content-Type", "application/vnd.ms-excel;charset=UTF-8");
		resp.setHeader("Content-Disposition", "attachment;filename=" + new String("GameTagExport.xls".getBytes(), "UTF-8"));
		OutputStream os = resp.getOutputStream();
		
		List<String[]> list = new ArrayList<String[]>();
		list = GameServiceImpl.getInstance().getGameNameAndLabel(appId, loginUserId);
		WritableWorkbook excel = ExcelUtils.ExportDataExcel(list,os);
		mv.addObject("excel", excel);
		return mv;
	}

	@RequestMapping(value = "/export", method = {RequestMethod.GET,RequestMethod.POST})
	public void ExportGameInfo(HttpServletRequest req, HttpServletResponse resp) throws Exception{		
		int gamestatus = WebUtils.getInt(req,"gamestatus",GameStatus.business.value());
		resp.reset();
		resp.setHeader("Content-Type", "application/vnd.ms-excel;charset=UTF-8");
		resp.setHeader("Content-Disposition", "attachment;filename=" + new String("GameInfoExport.xls".getBytes(), "UTF-8"));		
		WritableWorkbook excel = ExcelUtils.ExportGameInfo(resp.getOutputStream(), GameStatus.lookup(gamestatus));
		
	}

	@RequestMapping(value = "/image/download", method = { RequestMethod.GET,RequestMethod.POST })
	public void downloadImage(HttpServletRequest req, HttpServletResponse resp)	throws Exception {	
		if(req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY) != null){
			UserBean userBean = (UserBean) req.getSession().getAttribute(com.egame.manage.biz.utils.ConstVar.USER_SESSION_KEY);
			loginUserId = userBean.getOperatorId();
		}
		//String path = WebUtils.getString(req, "path", "");		
		String name = WebUtils.getString(req, "name", "");
		long efsid = WebUtils.getLong(req, "id", 0);
		String path = cn.egame.interfaces.ConstVar.UPLOAD_ADDRESS + GameServiceImpl.getInstance().getLocalFilePath(appId, loginUserId, efsid);
		resp.reset();
		resp.setContentType("application/x-download");
		resp.addHeader("Content-Disposition","attachment;filename=" + java.net.URLEncoder.encode(name,"UTF-8"));    
		try {
			//URL url = new URL(path);
	        //URLConnection conn = url.openConnection();
			//InputStream ip = conn.getInputStream();	
			FileInputStream ip = new FileInputStream(path);
			OutputStream op = resp.getOutputStream();				
			byte[] buffer = new byte[1204];
			int byteread = 0;
			while ((byteread = ip.read(buffer)) != -1) {
				op.write(buffer, 0, byteread);	
			}				
			op.flush();
			op.close();
			ip.close();				
			
		} catch (Exception e) {
			e.printStackTrace();
		} 			
		//return showGameInfo(req, resp);		
	}
	
	@RequestMapping(value = "/TCL/appid", method = { RequestMethod.GET,RequestMethod.POST })
	public ModelAndView getTclAppId(HttpServletRequest req, HttpServletResponse resp) throws Exception{
		int gid = WebUtils.getInt(req, "gid", 0);
		List<String[]> packageNameList = GameFileServiceImpl.getInstance().listPackageName(gid);		
		String tclAppId="";
		if(packageNameList != null && packageNameList.size()>0){
			for(String[] packageName:packageNameList){
				if(packageName != null && packageName.length == 2){
					tclAppId=GameServiceImpl.getInstance().getTCLAppIdByPackageName(packageName[1]);
					if(!Utils.stringIsNullOrEmpty(tclAppId)){
						GameServiceImpl.getInstance().updateTCLAppId(Integer.valueOf(packageName[0]), tclAppId);
					}
				}
			}
		}		
		return listGameManage(req,resp);
	}
}
